package com.example.evanproject.service.impl;


import cn.hutool.db.sql.Condition;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.evanproject.config.UpdateImg;
import com.example.evanproject.domain.Register;
import com.example.evanproject.service.RegisterService;
import com.example.evanproject.mapper.RegisterMapper;
import com.example.evanproject.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author hanxiaoqi
* @description 针对表【register】的数据库操作Service实现
* @createDate 2024-03-09 21:52:13
*/
@Service
public class RegisterServiceImpl extends ServiceImpl<RegisterMapper, Register>
    implements RegisterService{


    @Autowired
    RegisterMapper registerMapper;
    @Autowired
    private Getphonenum getphonenum;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /*获取验证码 验证验证码*/
    public ReturnsData getcode(String phnonenum){

        ReturnsData returnsData=new ReturnsData();
        if(getphonenum.isphone(phnonenum)){
            returnsData.setCode(200);
            returnsData.setMsg("获取成功");
            returnsData.setData(getphonenum.codegenerator());
            return returnsData;

        }else {
            returnsData.setCode(400);
            returnsData.setMsg("手机号格式不正确");
            return returnsData;

        }

    }


    /*注册用户*/
    public ReturnsData register(String phonenum,String code,String password,Integer sign){
        ReturnsData returnsData=new ReturnsData();
        /*验证手机验证码是否正确*/
        String value= stringRedisTemplate.opsForValue().get(phonenum);
        if(!code.equals(value)){
            /*检验手机验证码*/
            returnsData.setMsg("手机验证码不正确");
            return returnsData;
        }else {

            if(registerMapper.selectbyphone(phonenum)!=null){
                returnsData.setCode(400);
                returnsData.setMsg("用户已存在！！");
            }else {

                /*注册给个默认的头像和用户名*/
                Register register=new Register();
                register.setSAge(21);
                register.setSImg("evan.png");
                register.setSName("用户"+phonenum);
                register.setSSex(1);
                register.setSPassword(password);
                register.setSSign(sign);
                register.setSPhone(phonenum);
                returnsData.setCode(200);
                returnsData.setMsg("注册成功");
                returnsData.setData(registerMapper.register(register));
            }

        }

        return returnsData;
    }

    //用户登录
    public ReturnsData login(String phonenum,String password,String code){
        /*首先先检查用户是否存在*/
        ReturnsData returnsData=new ReturnsData();
        if(registerMapper.selectbyphone(phonenum)==null){
            returnsData.setCode(400);
            returnsData.setMsg("用户不存在");
            return returnsData;
        }else {
            //验证码登录
            if(code!=null&&code!=""){
                System.out.println(code);
                //取出来验证码 验证码相同登录
                /*验证手机验证码是否正确*/
                String value= stringRedisTemplate.opsForValue().get(phonenum);
                if(!code.equals(value)){
                    returnsData.setCode(400);
                    returnsData.setMsg("手机验证码不正确");
                    return returnsData;
                } else {
                    System.out.println(registerMapper.login(phonenum,password));
                    Register register2=registerMapper.login1(phonenum);
                    if(register2!=null){

                        returnsData.setCode(200);
                        returnsData.setMsg("登录成功");
                        //验证通过的时候要生成一个token
                        String token= TokenUtils.createToken( register2.getSId().toString(),register2.getSPassword());
                        //将token放到接口里面
                        Map<String,Object> map= EntityMapUtils.entityToMap(register2);
                        map.put("token",token);
                        System.out.println(map);
                        returnsData.setData(map);


                    }

                }
            }
            //密码登录
            if(password!=null&&password!=""){
                if(registerMapper.login(phonenum,password)!=null){
                    returnsData.setCode(200);
                    returnsData.setMsg("登录成功");

                    //验证通过的时候要生成一个token
                    Register register=registerMapper.login1(phonenum);
                    String token= TokenUtils.createToken( register.getSId().toString(),register.getSPassword());
                    //将token放到接口里面
                    Map<String,Object> map= EntityMapUtils.entityToMap(register);
                    map.put("token",token);
                    System.out.println(map);
                    returnsData.setData(map);

                }
                else {
                    returnsData.setCode(400);
                    returnsData.setMsg("密码错误");

                }

            }

            return returnsData;

        }


    }


    public Map updateinfo(String name, Integer age, Integer sex, MultipartFile file, Integer id) {
        String filename="";
        if(file!=null){
            filename= UpdateImg.updateimg(file);
        }

//返回一个修改后的信息
        Register register=new Register();
        if(registerMapper.modifyinfolist(name,age,sex,filename,id)>0){
            register=registerMapper.selectById(id);
        }

        Map<String,Object> map= EntityMapUtils.entityToMap(register);
        map.put("token",TokenUtils.createToken(register.getSId().toString(),register.getSPassword()));
        return map;
    }

    //查询用户(按照id查询)
    public Register selectById(Integer id) {
        return  registerMapper.selectById(id);
    }

    @Override
    public Integer[] countNum(String year) {

        if(year==null||year.equals("")){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
             year = sdf.format(new Date());
        }
        /*如果year为null 则默认为当前年份*/
        List<Map> list=registerMapper.countNum(year);

        Integer[] ids1 = {0,0,0,0,0,0,0,0,0,0,0,0};
        for (Map item:list) {
            if(item.get("month")!=null&&!item.get("month").equals("")){
                ids1[(Integer)item.get("month")-1]= Integer.valueOf(String.valueOf(item.get("sumnum"))) ;
            }

        }

        return ids1;
    }

    public R<IPage<Register>> selectAllLIstInfo(Register register) {

        QueryWrapper<Register> wrapper  =new QueryWrapper<>();
        wrapper.lambda().like(register.getSName()!=null&&!register.getSName().equals(""),Register::getSName,register.getSName())
                .between(register.getParamsQuery().get("begTime")!=null&&register.getParamsQuery().get("endTime")!=null,
                        Register::getSTime,register.getParamsQuery().get("begTime"),register.getParamsQuery().get("endTime"));
//        List<Register> list=registerMapper.selectList(wrapper);

        IPage<Register> page=new Page<>();
        page.setCurrent(Long.valueOf((String) register.getParamsQuery().get("pageNum")));
        page.setSize(Long.valueOf((String) register.getParamsQuery().get("pageSize")));
        IPage<Register> pages = registerMapper.selectPage(page, wrapper);
        return R.ok(pages);

    }
}




